/*-----------------------------------------------------------------------------------*/
/* Nuvoton Technology Corporation confidential                                       */
/*                                                                                   */
/* Copyright (c) 2008 by Nuvoton Technology Corporation                              */
/* All rights reserved                                                               */
/*                                                                                   */
/*-----------------------------------------------------------------------------------*/
/****************************************************************************
 * 
 * FILENAME
 *     sic.c
 *
 * VERSION
 *     1.0
 *
 * DESCRIPTION
 *     This file contains SIC library APIs.
 *
 * DATA STRUCTURES
 *     None
 *
 * FUNCTIONS
 *     None
 *
 * HISTORY
  *     10/11/07      Create Ver 1.0
 *
 * REMARK
 *     None
 **************************************************************************/
/* Header files */
#include <stdio.h>
#include <string.h>

#include "wblib.h"
#include "nuc900_sic.h"

/*-----------------------------------------------------------------------------------*/
/* Function:                                                                         */
/*   sicOpen                                                                         */
/*                                                                                   */
/* Parameters:                                                                       */
/*   None				                                                             */
/*                                                                                   */                                                                           
/* Returns:                                                                          */
/*   None.                                                                           */
/*                                                                                   */
/* Side effects:                                                                     */
/*   None.                                                                           */
/*                                                                                   */
/* Description:                                                                      */
/*   This function reset the DMAC and SIC interface and enable interrupt.            */
/*                                                                                   */
/*-----------------------------------------------------------------------------------*/
static _fmi_init_flag = FALSE;
void sicOpen(VOID)
{
	if (!_fmi_init_flag)
	{
		fmiInitDevice();
		_fmi_init_flag = TRUE;
	}
}

/*-----------------------------------------------------------------------------------*/
/* Function:                                                                         */
/*   sicSdOpen                                                                       */
/*                                                                                   */
/* Parameters:                                                                       */
/*   sdPortNo				SD port number(port0 or port1).							 */
/*                                                                                   */                                                                           
/* Returns:                                                                          */
/*   >0						Total sector.											 */
/*   FMI_NO_SD_CARD			No SD card insert.										 */
/*   FMI_SD_INIT_ERROR		Card initial and identify error.						 */
/*   FMI_SD_SELECT_ERROR	Select card from identify mode to stand-by mode error.	 */
/*                                                                                   */                                                                           
/* Side effects:                                                                     */
/*   None.                                                                           */
/*                                                                                   */
/* Description:                                                                      */
/*   This function initial SD from identification to stand-by mode.                  */
/*                                                                                   */
/*-----------------------------------------------------------------------------------*/

INT sicSdOpen(UINT32 sdPortNo)
{
	return fmiInitSDDevice(sdPortNo);
}

/*-----------------------------------------------------------------------------------*/
/* Function:                                                                         */
/*   sicMsOpen                                                                       */
/*                                                                                   */
/* Parameters:                                                                       */
/*   msPortNo				MS port number(port0 or port1).							 */
/*                                                                                   */                                                                           
/* Returns:                                                                          */
/*   >0						Total sector.											 */
/*   FMI_NO_MS_CARD			No MS card insert.										 */
/*   FMI_MS_INIT_ERROR		Get Card information error.								 */
/*                                                                                   */
/* Side effects:                                                                     */
/*   None.                                                                           */
/*                                                                                   */
/* Description:                                                                      */
/*   This function initial the MS card.												 */
/*                                                                                   */
/*-----------------------------------------------------------------------------------*/

INT sicMsOpen(UINT32 msPortNo)
{
	return fmiInitMSDevice(msPortNo);
}

/*-----------------------------------------------------------------------------------*/
/* Function:                                                                         */
/*   sicNandOpen                                                                     */
/*                                                                                   */
/* Parameters:                                                                       */
/*   nandPortNo				NAND chip select(port0 or port1).						 */
/*                                                                                   */                                                                           
/* Returns:                                                                          */
/*   >0						Total sector.											 */
/*   FMI_SM_INIT_ERROR		Get NAND ID error.										 */
/*                                                                                   */
/* Side effects:                                                                     */
/*   None.                                                                           */
/*                                                                                   */
/* Description:                                                                      */
/*   This function initial NAND flash.												 */
/*                                                                                   */
/*-----------------------------------------------------------------------------------*/

INT sicNandOpen(UINT32 nandPortNo)
{
	return 0;
}

/*-----------------------------------------------------------------------------------*/
/* Function:                                                                         */
/*   sicIoctl                                                                        */
/*                                                                                   */
/* Parameters:                                                                       */
/*   sicFeature		SIC_SET_CLOCK, SIC_SET_CALLBACK.								 */
/*   sicArg0		Depend on feature setting.										 */
/*   sicArg1		Depend on feature setting.										 */
/*   sicArg2		Depend on feature setting.										 */
/*                                                                                   */                                                                           
/* Returns:                                                                          */
/*   None.                                                                           */
/*                                                                                   */
/* Side effects:                                                                     */
/*   None.                                                                           */
/*                                                                                   */
/* Description:                                                                      */
/*   This function set the FMI engine clock. Card insert/remove callback             */
/*	SIC_SET_CLOCK : sicArg0 used to set clock by KHz								 */
/*                                                                                   */
/*	SIC_SET_CALLBACK : sicArg0 used to select card type (FMI_SD_CARD0, FMI_SD_CARD1, */
/*														FMI_MS_CARD0, FMI_MS_CARD1)  */
/*	SIC_SET_CALLBACK : sicArg1 remove function pointer								 */
/*	SIC_SET_CALLBACK : sicArg2 insert function pointer								 */
/*-----------------------------------------------------------------------------------*/

VOID sicIoctl(INT32 sicFeature, INT32 sicArg0, INT32 sicArg1, INT32 sicArg2)
{
	switch(sicFeature)
	{
		case SIC_SET_CLOCK:
			fmiSetFMIReferenceClock(sicArg0);
			break;
		
		case SIC_SET_CALLBACK:
			fmiSetCallBack(sicArg0, (PVOID)sicArg1, (PVOID)sicArg2);
			break;
	}
}

/*-----------------------------------------------------------------------------------*/
/* Function:                                                                         */
/*   sicSdRead                                                                       */
/*                                                                                   */
/* Parameters:                                                                       */
/*   sicSdPortNo	Select SD port 0 or port 1.										 */
/*   sdSectorNo		Sector No. to get the data from.								 */
/*   sdSectorCount	Sector count of this access.									 */
/*   sdTargetAddr	The address which data upload to SDRAM.							 */
/*                                                                                   */                                                                           
/* Returns:                                                                          */
/*   0					Success                                                      */
/*   FMI_TIMEOUT        Access timeout                                               */
/*   FMI_NO_SD_CARD     Card removed                                                 */
/*   FMI_SD_CRC7_ERROR  Command/Response error                                       */
/*   FMI_SD_CRC16_ERROR Data transfer error                                          */
/*                                                                                   */
/* Side effects:                                                                     */
/*   None.                                                                           */
/*                                                                                   */
/* Description:                                                                      */
/*   This function used to get the data from SD card					             */
/*-----------------------------------------------------------------------------------*/

INT sicSdRead(INT32 sicSdPortNo, INT32 sdSectorNo, INT32 sdSectorCount, INT32 sdTargetAddr)
{
	return fmiSD_Read(sicSdPortNo, sdSectorNo, sdSectorCount, sdTargetAddr);
}

/*-----------------------------------------------------------------------------------*/
/* Function:                                                                         */
/*   sicSdWrite                                                                      */
/*                                                                                   */
/* Parameters:                                                                       */
/*   sicSdPortNo	Select SD port 0 or port 1.										 */
/*   sdSectorNo		Sector No. to get the data from.								 */
/*   sdSectorCount	Sector count of this access.									 */
/*   sdSourcetAddr	The address which data download data from SDRAM.				 */
/*                                                                                   */                                                                           
/* Returns:                                                                          */
/*   0					Success                                                      */
/*   FMI_TIMEOUT        Access timeout                                               */
/*   FMI_NO_SD_CARD     Card removed                                                 */
/*   FMI_SD_CRC7_ERROR  Command/Response error                                       */
/*   FMI_SD_CRC_ERROR   Data transfer error                                          */
/*                                                                                   */
/* Side effects:                                                                     */
/*   None.                                                                           */
/*                                                                                   */
/* Description:                                                                      */
/*   This function used to write the data to SD card					             */
/*-----------------------------------------------------------------------------------*/

INT sicSdWrite(INT32 sicSdPortNo, INT32 sdSectorNo, INT32 sdSectorCount, INT32 sdSourceAddr)
{
	return fmiSD_Write(sicSdPortNo, sdSectorNo, sdSectorCount, sdSourceAddr);
}

/*-----------------------------------------------------------------------------------*/
/* Function:                                                                         */
/*   sicNandRead                                                                     */
/*                                                                                   */
/* Parameters:                                                                       */
/*   sicNandPortNo		Select NAND port 0 or port 1.								 */
/*   nandSectorNo		Sector No. to get the data from.							 */
/*   nandSectorCount	Sector count of this access.								 */
/*   nandTargetAddr		The address which data upload to SDRAM.						 */
/*                                                                                   */                                                                           
/* Returns:                                                                          */
/*   0					Success                                                      */
/*   FMI_SM_ECC_ERROR   Read back data ECC error                                     */
/*                                                                                   */
/* Side effects:                                                                     */
/*   None.                                                                           */
/*                                                                                   */
/* Description:                                                                      */
/*   This function used to get the data from Nand flash					             */
/*-----------------------------------------------------------------------------------*/

INT sicNandRead(INT32 sicNandPortNo, INT32 nandSectorNo, INT32 nandSectorCount, INT32 nandTargetAddr)
{
	return 0;
}

/*-----------------------------------------------------------------------------------*/
/* Function:                                                                         */
/*   sicNandWrite                                                                    */
/*                                                                                   */
/* Parameters:                                                                       */
/*   sicNandPortNo		Select Nand port 0 or port 1.								 */
/*   nandSectorNo		Sector No. to get the data from.							 */
/*   nandSectorCount	Sector count of this access.								 */
/*   nandSourcetAddr	The address which data download data from SDRAM.			 */
/*                                                                                   */                                                                           
/* Returns:                                                                          */
/*   0						Success                                                  */
/*   FMI_SM_RB_ERR      	Nand flash busy                                          */
/*   FMI_SM_STATE_ERROR  	write error                                              */
/*                                                                                   */
/* Side effects:                                                                     */
/*   None.                                                                           */
/*                                                                                   */
/* Description:                                                                      */
/*   This function used to write the data to Nand flash					             */
/*-----------------------------------------------------------------------------------*/

INT sicNandWrite(INT32 sicNandPortNo, INT32 nandSectorNo, INT32 nandSectorCount, INT32 nandSourceAddr)
{
	return 0;
}

/*-----------------------------------------------------------------------------------*/
/* Function:                                                                         */
/*   sicMsRead                                                                       */
/*                                                                                   */
/* Parameters:                                                                       */
/*   sicMsPortNo	Select MS port 0 or port 1.										 */
/*   msSectorNo		Sector No. to get the data from.								 */
/*   msSectorCount	Sector count of this access.									 */
/*   msTargetAddr	The address which data upload to SDRAM.							 */
/*                                                                                   */                                                                           
/* Returns:                                                                          */
/*   0						Success                                                  */
/*   FMI_NO_MS_CARD        	                                                         */
/*   FMI_MS_BUSY_TIMEOUT                                                             */
/*   FMI_MS_CRC_ERROR  		                                                         */
/*   FMI_MS_INT_TIMEOUT 	                                                         */
/*   FMI_MS_INT_CMDNK 		                                                         */
/*   FMI_MS_INT_ERR 		                                                         */
/*   FMI_MS_INT_BREQ 		                                                         */
/*   FMI_MS_WRONG_SEGMENT 		                                                     */
/*   FMI_MS_WRONG_PHYBLOCK 		                                                     */
/*                                                                                   */
/* Side effects:                                                                     */
/*   None.                                                                           */
/*                                                                                   */
/* Description:                                                                      */
/*   This function used to get the data from MS card					             */
/*-----------------------------------------------------------------------------------*/

INT sicMsRead(INT32 sicMsPortNo, INT32 msSectorNo, INT32 msSectorCount, INT32 msTargetAddr)
{
	return fmiMS_Read(sicMsPortNo, msSectorNo, msSectorCount, msTargetAddr);
}

/*-----------------------------------------------------------------------------------*/
/* Function:                                                                         */
/*   sicMsWrite                                                                      */
/*                                                                                   */
/* Parameters:                                                                       */
/*   sicMsPortNo	Select MS port 0 or port 1.										 */
/*   msSectorNo		Sector No. to get the data from.								 */
/*   msSectorCount	Sector count of this access.									 */
/*   msSourcetAddr	The address which data download data from SDRAM.				 */
/*                                                                                   */                                                                           
/* Returns:                                                                          */
/*   0						Success                                                  */
/*   FMI_NO_MS_CARD        	                                                         */
/*   FMI_MS_BUSY_TIMEOUT                                                             */
/*   FMI_MS_CRC_ERROR  		                                                         */
/*   FMI_MS_INT_TIMEOUT 	                                                         */
/*   FMI_MS_INT_CMDNK 		                                                         */
/*   FMI_MS_INT_ERR 		                                                         */
/*   FMI_MS_INT_BREQ 		                                                         */
/*   FMI_MS_WRONG_SEGMENT 		                                                     */
/*   FMI_MS_WRONG_PHYBLOCK 		                                                     */
/*                                                                                   */
/*                                                                                   */
/* Side effects:                                                                     */
/*   None.                                                                           */
/*                                                                                   */
/* Description:                                                                      */
/*   This function used to write the data to MS card					             */
/*-----------------------------------------------------------------------------------*/

INT sicMsWrite(INT32 sicMsPortNo, INT32 msSectorNo, INT32 msSectorCount, INT32 msSourceAddr)
{
	return fmiMS_Write(sicMsPortNo, msSectorNo, msSectorCount, msSourceAddr);
}





